package interval

import (
	"time"
)

type IntervalEngineer interface {
	OpenInterval(id interface{}, interval time.Duration, fn func()) (err error)
	CloseInterval(id interface{}) error
}

func SetDefaultIntervalEngineer(e IntervalEngineer) {
	if e == nil {
		panic("defaultIntervalEngine cannot be nil")
	}
	defaultIntervalEngineer = e
}

var (
	defaultIntervalEngineer IntervalEngineer
)

func OpenInterval(id interface{}, interval time.Duration, fn func()) (err error) {
	return defaultIntervalEngineer.OpenInterval(id, interval, fn)
}

func CloseInterval(id interface{}) error {
	return defaultIntervalEngineer.CloseInterval(id)
}

func init() {

	SetDefaultIntervalEngineer(NewIntervalLockMapEngine())
}
